package com.ctrlcar.giftrecord.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ctrlcar.giftrecord.mapper.RelativeMapper;
import com.ctrlcar.giftrecord.pojo.ReceiveGift;
import com.ctrlcar.giftrecord.pojo.Relative;
import com.ctrlcar.giftrecord.pojo.SendGift;
import com.ctrlcar.giftrecord.pojo.model.Result;
import com.ctrlcar.giftrecord.service.ReceiveGiftService;
import com.ctrlcar.giftrecord.service.RelativeService;
import com.ctrlcar.giftrecord.service.SendGiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author
* @description 针对表【relative】的数据库操作Service实现
* @createDate 2025-03-26 16:43:36
*/
@Service
public class RelativeServiceImpl extends ServiceImpl<RelativeMapper, Relative>
    implements RelativeService {
    @Resource
    private ReceiveGiftService receiveGiftService;

    @Lazy
    @Resource
    private SendGiftService sendGiftService;

    /**
     * 根据relativeId删除relative
     * @param relativeId
     * @return
     */
    @Transactional
    @Override
    public Result delectByrelativeId(Integer relativeId) {
        //删除亲朋的送礼
        LambdaQueryWrapper<ReceiveGift> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReceiveGift::getRelativeId,relativeId);
        receiveGiftService.remove(queryWrapper);
        //删除亲朋的收礼
        LambdaQueryWrapper<SendGift> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(SendGift::getRelativeId,relativeId);
        sendGiftService.remove(queryWrapper1);
        //删除亲朋
        boolean b = this.removeById(relativeId);
        if (b){
            return  Result.success();
        }
        return  Result.error("删除有误");
    }
}




